package model;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import tree.Tree;


public class Data {
	
	 public enum Phase {
		   Train , Predict;
	}
	private int days;
	private ArrayList<Stock> allStocks ; 
	private Stock currentStock;
	private String modelfile;
	private String buyfile;
	private Phase phase ;
	private Data() {
		days = 0;
		allStocks =  new ArrayList<Stock>(); 
		currentStock = null;
	}
	
	/**
	 * Singleton Pattern
	 * @return the only instance of Data
	 */	
	public static Data get(){ // lazy
		return Holder.theData;
	}
	private static class Holder{
		static final Data theData = new Data();		
	}
	
	//-----------------Getters && Setters----------------//
	public void setDays(int days) {
		this.days = days;
	}
	public int getDays() {
		return days;
	}
	public void setAllStocks(ArrayList<Stock> allStocks) {
		this.allStocks = allStocks;
	}
	public ArrayList<Stock> getAllStocks() {
		return allStocks;
	}
	public void setCurrentStock(Stock currentStock) {
		this.currentStock = currentStock;
	}
	public Stock getCurrentStock() {
		return currentStock;
	}

	public void setModelfile(String modelfile) {
		this.modelfile = modelfile;
	}

	public String getModelfile() {
		return modelfile;
	}
	
	
	public Tree getSavedTree(){
		
		XMLDecoder decoder = null;
		Tree savedTree=null;
		
		try{
			decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(modelfile+".xml")));
			savedTree=(Tree)decoder.readObject();
		}catch (FileNotFoundException e) {			
			e.printStackTrace();
		}finally{
			if (decoder!=null){
				decoder.close();
			}			
		}
		
		return savedTree;
	}

	public void setBuyfile(String buyfile) {
		this.buyfile = buyfile;
	}

	public String getBuyfile() {
		return buyfile;
	}

	public void setPhase(Phase phase) {
		this.phase = phase;
	}

	public Phase getPhase() {
		return phase;
	}
}
